package com.ljy.my_study.spring.tiny.study8.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.aopalliance.intercept.MethodInterceptor;

public class JdkDynamicAopProxy implements  AopProxy,InvocationHandler{

	private AdvisedSupport advisedSupport;
	
	public JdkDynamicAopProxy(AdvisedSupport advisedSupport) {
		this.advisedSupport=advisedSupport;
	}
	
	@Override
	public Object getProxy() {
		return Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {
				advisedSupport.getTargetSource().getTargetClass()
		}, this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		MethodInterceptor interceptor=advisedSupport.getMethodInterceptor();
		return interceptor.invoke(
				new RefectiveMethodInvocation(advisedSupport.getTargetSource().getTarget(),method,args));
	}

}
